import Vue from "vue";
//弹框提示
Vue.prototype.prompt = (title, content , showCancel = true) => {
	return new Promise(r => {
			uni.showModal({
				title: content ? title : '提示',
				content: content || title,
				showCancel,
				success: (res) => {
					if (res.confirm) {
						r(true);
					} else if (res.cancel) {
						r();
					}
				}
			});
		});
}
Vue.prototype.log = e => {
	console.log(e);
}

//全局随机数重写
const random = Math.random;
Math.random = (a = 0, b = 1, c = 6) => {
	return (random() * (b - a) + a).toFixed(c) - 0;
};
//全局重写四舍五入函数 可传入保留位数
const round = Math.round;
Math.round = (a = 0, b = 0) => {
	b = b - 0 || 0;
	const m = Math.pow(10, b);
	return round(a * m) / m;
}
//数组去重
Array.prototype.setArray = function() {
	return Array.from(new Set(this));
}

/**
 * RGB转16进
 */
String.prototype.colorHex = function() {
	// RGB颜色值的正则
	var reg = /^(rgb|RGB)/;
	var color = this;
	if (reg.test(color)) {
		var strHex = "#";
		// 把RGB的3个数值变成数组
		var colorArr = color.replace(/(?:\(|\)|rgb|RGB)*/g, "").split(",");
		// 转成16进制
		for (var i = 0; i < colorArr.length; i++) {
			var hex = Number(colorArr[i]).toString(16);
			if (hex === "0") {
				hex += hex;
			}
			strHex += hex;
		}
		return strHex;
	} else {
		return String(color);
	}
};
/**
 * 16进制转RGB
 */
String.prototype.colorRgb = function(ap = 1) {
	// 把颜色值变成小写
	var color = this.toLowerCase();
	if (~color.indexOf('rgb')) {
		color = this.colorHex();
	}
	// 16进制颜色值的正则
	var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
	if (reg.test(color)) {
		// 如果只有三位的值，需变成六位，如：#fff => #ffffff
		if (color.length === 4) {
			var colorNew = "#";
			for (var i = 1; i < 4; i += 1) {
				colorNew += color.slice(i, i + 1).concat(color.slice(i, i + 1));
			}
			color = colorNew;
		}
		// 处理六位的颜色值，转为RGB
		var colorChange = [];
		for (var i = 1; i < 7; i += 2) {
			colorChange.push(parseInt("0x" + color.slice(i, i + 2)));
		}
		return `RGBA(${colorChange.join(",")},${ap})`;
	} else {
		return color;
	}
};



//h5 专用加载
// 插入样式
const s_name = new Date().getTime();
document.write(
	`
<style type="text/css">
	@keyframes spin${s_name} {
		from {
			transform: rotate(0deg);
		}
	
		to {
			transform: rotate(360deg);
		}
	}
	
	.loading${s_name} {
		position: relative;
		font-size: 12px;
		display: flex;
		justify-content: center;
		align-items: center;
		color:#fff;
		height:5em;
		width:5em;
	}
	.loading${s_name}::after{
		content: '';
		position: absolute;
		top: 0;
		bottom: 0;
		left: 0;
		right: 0;
		border-radius: 50%;
		border-top: 3px solid #ff007f;
		border-bottom: 3px solid #00aaff;
		border-left: 3px solid rgba(0, 0, 0, 0);
		border-right: 3px solid rgba(0, 0, 0, 0);
		animation: spin${s_name} 1s linear infinite;
		/*background-color:rgba(255,255,255,.6);*/
	}
</style>
`
);
export const loading = function({
	el = document.body,
	msg = "加载中",
	time = 0
} = {}) {
	const {
		$el
	} = (this || {});
	el = $el || el;
	let div = document.createElement('div');
	div.classList.add(`loading${s_name}`);
	let box = document.createElement('div');
	box.style.display = "flex";
	box.style.top = 0;
	box.style.bottom = 0;
	box.style.left = 0;
	box.style.right = 0;
	box.style.zIndex = 99999;
	box.style.backgroundColor = "rgba(0,0,0,.4)";
	box.style.justifyContent = "center";
	box.style.alignItems = "center";
	box.style.textAlign = "center";
	box.appendChild(div);
	div.innerText = msg;

	function countdown(time) {
		if (time-- > 1) {
			div.innerText = msg + time + "s";
			setTimeout(e => {
				countdown(time);
			}, 1000);
		} else {
			box.remove();
		}
	};
	if (time) {
		countdown(time / 1000);
		box.onclick = box.remove;
	}

	if (el === document.body) {
		box.style.position = "fixed";
	} else {
		box.style.position = "absolute";
	}
	el.appendChild(box);
	return function() {
		box.remove();
	}
}
